<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <style>
    .circle {
      fill: none;
      stroke: #7c83fd;
      stroke-width: 8;
      stroke-dasharray: 314;
      stroke-dashoffset: 314;
      stroke-linecap: round;
      transition: all 1s;
    }
    .text {
      font-size: 20px;
    }
    .percent {
      font-size: 10px;
    }
  </style>
</head>
<body>
<svg>
<!--  <circle> 标签可用来创建一个圆。
cx 和 cy 属性定义圆点的 x 和 y 坐标。如果省略 cx 和 cy，圆的中心会被设置为 (0, 0)
r 属性定义圆的半径。
-->
  <circle
          class="circle"
          cx="80"
          cy="80"
          r="50"
          stroke="#7c83fd"
          transform="rotate(-90 80 80)"
  ></circle>
  <text x="80" y="85" fill="#6b778c" text-anchor="middle">
    <tspan class="text">0</tspan>
    <tspan class="percent">%</tspan>
  </text>
</svg>
</body>
<script>
  let progressLen = 314;
  const textDom = document.getElementsByClassName('text')[0];
  const circleDom = document.getElementsByClassName('circle')[0];

  setPercent = num => {
    //? 进度条到达100
    if (num > 100) return;
    //? stroke-dashoffset指圆形描边线条边缘与圆形起点之间的距离(即环形进度条中为填充部分的长度)
    circleDom.style['stroke-dashoffset'] = progressLen - (progressLen / 100) * num;
    textDom.innerHTML = num;
  };

  let i = 0;
  //? 定时调用setPercent函数 模拟进度条动的效果
  setInterval(() => {
    i += Math.floor(Math.random() * 5);
    if (i >= 100) i = 100;
    setPercent(i);
  }, 250);
</script>
</html>
